package servlets;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import capaLogica.Comentario;
import capaLogica.ComentarioBO;
import capaLogica.Evento;
import capaLogica.EventoBO;
import capaLogica.Multimedia;
import capaLogica.MultimediaBO;
import capaLogica.Usuario;

/**
 * Servlet implementation class ComentarioCO
 */
public class ComentarioCO extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ComentarioCO() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String accion = request.getParameter("accion");
		try {
			procesarAccion(accion, request, response);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void procesarAccion(String accion, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException, ParseException {
		if (accion.equals("crearComentario")) {
			crearComentario(request, response);
		}else if ( accion.equals("votar") ){
			votar(request,response);
		}
	}

	private void votar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idEvento = request.getParameter("idEvento");
		String voto = request.getParameter("positivo");
		
		EventoBO eveBO = new EventoBO();
		Evento e = eveBO.recuperarEventoById(idEvento);
		if ( "1".equals(voto) ){
			e.setVotos(e.getVotos()+1);
		}else{
			e.setVotos(e.getVotos()-1);
		}
		
		eveBO.actualizarEvento(e);
		request.getRequestDispatcher("evento?accion=verEvento&idEvento="+idEvento).include(request, response);
	}

	private void crearComentario(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String idEvento = request.getParameter("idEvento");
		String comentario = request.getParameter("comentario");
		HttpSession sesion = request.getSession();
		Usuario user = (Usuario)sesion.getAttribute("usuario");
		
		Comentario cm = new Comentario();
		cm.setDescripcion(comentario);
		cm.setFechaPublicacion(new Date());
		cm.setIdUsuarioCreador(user.getIdUsuario());
		
		ComentarioBO comentarioBO = new ComentarioBO();
		Comentario c1 = comentarioBO.insertarComentario(cm);
		
		comentarioBO.insertarAsociativa(idEvento, c1.getIdComentario());
		EventoBO eBO = new EventoBO();
		Evento evento = eBO.recuperarEventoById(idEvento);

		List<Comentario> listaC = comentarioBO.recuperarTodos();
		
		MultimediaBO multiBO = new MultimediaBO();
		List<Multimedia> listaFotos = multiBO.recuperarFotosByEvento(idEvento);
		List<Multimedia> listaVideo = multiBO.recuperarVideoByEvento(idEvento);
		
		request.setAttribute("listaComentario", listaC);
		request.setAttribute("listaFoto", listaFotos);
		request.setAttribute("listaVideo", listaVideo);
		request.setAttribute("evento", evento);
		request.getRequestDispatcher("visualizarEvento.jsp").include(request, response);
		
	}
}
